package com.shujia.scala

import scala.io.Source

object Demo29Student {
  def main(args: Array[String]): Unit = {

    /**
      *
      * 3、统计每科都及格的学生 [学号，姓名，班级，科目，分数]
      *
      */

    // 1、统计每隔学生及格科目数量

    val scores = Source.fromFile("scala/data/score.txt").getLines().toList
    val cources = Source.fromFile("scala/data/cource.txt").getLines().toList

    //科目总分
    val cSumScoreKV: Map[String, Int] = cources.map(line => {
      val split = line.split(",")
      (split(0), split(2).toInt)
    }).toMap

    //过滤及格的分数
    val filterScore = scores.filter(line => {
      val split = line.split(",")
      val id = split(0)
      val courceId = split(1)
      val score = split(2).toInt

      //关联科目表  获取科目的总分
      val courceSum = cSumScoreKV.getOrElse(courceId, 100)

      score > courceSum * 0.6
    })


    //统计每个学生及格科目的数量
    //以学号进行分组
    val groupByScore = filterScore.groupBy(line => line.split(",")(0))

    //所有科目都及格的学生
    val fScore = groupByScore.filter(kv => kv._2.size == 6)


    //将数据扁平化
    //map  传入一行返回一行
    //flatMap   传入一行返回多行
    val resultScore = fScore.flatMap(kv => kv._2)

    resultScore.foreach(println)

  }

}
